package com.twang.stock.design.impl;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.design.IndexDataTask;
import com.twang.stock.dto.IndexedInfo;
import com.twang.stock.entity.TodayQinXu;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.service.TodayQinXuService;
import com.twang.stock.service.impl.TodayQinXuServiceImpl;
import com.twang.stock.utils.date.HolidayCopy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

/**
 * 大盘涨跌情况
 */

@Service
@Slf4j
public class ZhangDieTingTask   extends IndexedInfo implements IndexDataTask {
    public static String cache_key = "ZhangDieTingTask";

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private TodayQinXuService todayQinXuService;
    @Override
    public void execute(ModelAndView modelAndView, CountDownLatch latch) throws Exception {
        try {
            TodayQinXu param = new TodayQinXu();
            param.setDate(HolidayCopy.getRealToday());
            //查询
            Object obj = redisUtil.get(cache_key);
            List<TodayQinXu> result;
            if (null == obj) {
                result = todayQinXuService.queryList(param, TodayQinXuServiceImpl.TYPE_SHORT_ASC);
            } else {
                result = JSONObject.parseArray(obj.toString(), TodayQinXu.class);
            }
            //当日行情信息
            List<BigDecimal> zhangTingList = result.stream().map(item -> item.getNowZtzs()).collect(Collectors.toList());
            List<BigDecimal> dieTingList = result.stream().map(item -> item.getNowDtzs()).collect(Collectors.toList());
            List<BigDecimal> szList = result.stream().map(item -> item.getZtzs()).collect(Collectors.toList());
            List<BigDecimal> xdList = result.stream().map(item -> item.getDtzs()).collect(Collectors.toList());
            modelAndView.addObject("zhangTingList", zhangTingList);
            modelAndView.addObject("dieTingList", dieTingList);
            modelAndView.addObject("szList", szList);
            modelAndView.addObject("szListNum", szList.get(szList.size() - 1));
            modelAndView.addObject("xdList", xdList);
            modelAndView.addObject("sxdListNum", xdList.get(xdList.size() - 1));
            modelAndView.addObject("size", zhangTingList.size());
            log.info("大盘涨跌情况 = {}", JSONObject.toJSONString(param));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            log.info("大盘涨跌情况 执行完成，当前剩余条数:{}", latch.getCount());
            latch.countDown();
        }
    }
}
